/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.haipv.gtkhoa501.screen;

import com.haipv.gtkhoa501.controller.Event;
import com.haipv.gtkhoa501.model.Var;
import com.nokia.lwuit.components.HeaderBar;
import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Form;
import com.sun.lwuit.Graphics;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.Painter;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.geom.Rectangle;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.layouts.GridLayout;
import com.sun.lwuit.table.TableLayout;
import java.io.IOException;

/**
 *
 * @author PHANHAI
 * Giao diện hiển thị các loại biển báo giao thông
 */
public class MainSignsScreen extends BaseScreen implements ActionListener {

    Button[] buttons = new Button[6];
    Image img = null;
    Label lab[] = new Label[6];
    Command back = new Command(Var.back);

    public void showForm() {

        Form form = new Form();
        
        HeaderBar header = null;
        try {
            header = new HeaderBar(Var.trafficSigns);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        header.setHeaderTitleColor(Var.colorHearder);
        header.getStyle().setBgColor(Var.colorWhite);
        header.setScrollable(false);
        
        GridLayout exampleLayout = new GridLayout(3, 2);
        exampleLayout.setFillLastRow(true);
        //exampleLayout.setAutoFit(true);
        
        Container con = new Container();
        con.setLayout(exampleLayout);
        Container contai[] = new Container[6];
        


        for (int i = 0; i < 6; i++) {


            try {
                img = Image.createImage("/images/categoriBar/10"+(i+1)+".png");
                buttons[i] = new Button(img);

                buttons[i].getStyle().setPadding(0, 0, 0, 0);
                buttons[i].getUnselectedStyle().setPadding(0, 0, 0, 0);
                buttons[i].getSelectedStyle().setPadding(0, 0, 0, 0);
                buttons[i].getPressedStyle().setPadding(0, 0, 0, 0);

                buttons[i].getStyle().setMargin(0, 0, 0, 0);
                buttons[i].getSelectedStyle().setMargin(0, 0, 0, 0);
                buttons[i].getUnselectedStyle().setMargin(0, 0, 0, 0);
                buttons[i].getPressedStyle().setMargin(0, 0, 0, 0);

                buttons[i].addActionListener(this);

//                buttons[i].getStyle().setBgPainter(new Painter() {
//
//                    public void paint(Graphics g, Rectangle r) {
//                        g.drawImage(img, r.getX(), r.getY());
//                        System.out.println("r.getX()"+r.getX()+" r.getY()"+ r.getY());
//                    }
//                });
            } catch (IOException ex) {
                System.out.println("loi...");
                ex.printStackTrace();
            }
            contai[i] = new Container();
            

            lab[i] = new Label(Var.bienBao[i]);
//            contai1[i].addComponent(buttons[i]);
            contai[i].addComponent(buttons[i]);
//            contai[i].addComponent(BorderLayout.CENTER,lab[i]);

            contai[i].getStyle().setMargin(0, 0, 0, 0);
            contai[i].getSelectedStyle().setMargin(0, 0, 0, 0);
            contai[i].getUnselectedStyle().setMargin(0, 0, 0, 0);
            contai[i].getPressedStyle().setMargin(0, 0, 0, 0);
            
            contai[i].getStyle().setPadding(0, 0, 0, 0);
            contai[i].getSelectedStyle().setPadding(0, 0, 0, 0);
            contai[i].getUnselectedStyle().setPadding(0, 0, 0, 0);
            contai[i].getPressedStyle().setPadding(0, 0, 0, 0);
            
            //contai[i].getStyle().setAlignment(Container.LEFT);
//            contai[i].getStyle().setBgPainter(new Painter() {
//
//                public void paint(Graphics grphcs, Rectangle rctngl) {
//                    grphcs.setColor(0x000fff);
//                    grphcs.fillRect(rctngl.getX(), rctngl.getY(), rctngl.getSize().getWidth(),rctngl.getSize().getHeight());
//                }
//            });
            con.addComponent(contai[i]);


        }

//        con.getStyle().setMargin(0, 0, 0, 0);
//        con.getSelectedStyle().setMargin(0, 0, 0, 0);
//        con.getUnselectedStyle().setPadding(0, 0, 0, 0);
//        con.getPressedStyle().setPadding(0, 0, 0, 0);


        con.setScrollableY(true);
        con.getStyle().setMargin(0, 0, 0, 0);
        form.setLayout(new BorderLayout());
//        form.setTitle(Var.trafficSigns);
        form.addComponent(BorderLayout.NORTH, header);
        form.addComponent(BorderLayout.CENTER, con);
        form.setScrollable(false);

        form.setBackCommand(back);
        form.addCommand(back);
        form.addCommandListener(this);
        form.show();

    }

    public void actionPerformed(ActionEvent ae) {
        if (ae.getCommand() == back) {
            controller.handleEvent(Event.MAIN_ACTION, null);
        } else {
            for (int i = 0; i < 6; i++) {
                if (ae.getComponent() == buttons[i]) {

                    Event event = new Event();
                    event.setKey("TYPE", (i + 1) + "");
                    //   event.setKey("NAME", lab[i].getText());

                    controller.handleEvent(Event.SIGN_SELECT, event);
                    break;
                }

            }
        }
    }
}
